﻿using CTM.Web.Models.Input;
using CTM.Web.Utils;
using System;
using System.ComponentModel.DataAnnotations;
using System.Web;
using System.Web.Mvc;

namespace CTM.Web.Models.View.Notification
{
    public class NotificationViewModel
    {
        #region Properties

        [Key]
        public Guid Id { get; set; }

        public NotificationType Type { get; set; }

        public bool IsDeleted { get; set; }

        public DateTime CreatedOn { get; set; }

        [StringLength(128)]
        public string CreatedBy { get; set; }

        #endregion

        #region Relationship

        public virtual Post Post { get; set; }
        public virtual Input.Comment Comment { get; set; }
        public virtual ApplicationUser Giver { get; set; }
        public virtual ApplicationUser Receiver { get; set; }

        #endregion

        #region Automatic Properties

        public string Message
        {
            get
            {
                switch (Type)
                {
                    case NotificationType.Other: return string.Empty;
                    case NotificationType.Post: switch (Post.Category.Name)
                        {
                            case "Conversation": return string.Format("{0} started a new conversation with you.", Giver.DisplayName);
                            case "Feedback": return string.Format("{0} created a new feedback for you.", Giver.DisplayName);
                            default: return string.Empty;
                        }
                    case NotificationType.Comment: return string.Format("{0} replied to a post that you joined in.", Giver.DisplayName);
                    default: return string.Empty;
                }
            }
        }

        public string Url
        {
            get
            {
                var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
                string url = string.Empty;
                string hash = string.Empty; /* may need to be changed depended on situations */

                // Get post/comment category
                string category = string.Empty;
                switch (Type)
                {
                    case NotificationType.Post:
                        category = Post.Category.Name;
                        hash = Post.Id.ToString();
                        break;

                    case NotificationType.Comment:
                        category = Comment.Post.Category.Name;
                        break;
                }

                // generate the link by category
                switch (category)
                {
                    case "Conversation":
                        // which page (current/selected) does this conversations appear???
                        url = urlHelper.Action("Index", "Conversation", new { id = Receiver.Id, notification = Id });
                        break;

                    case "Feedback":
                        url = urlHelper.Action("Index", "Feedback", new { id = Receiver.Id, notification = Id });
                        hash = string.Format("feedback-{0}", Id);
                        break;
                }

                // return the generated link
                return string.Format("{0}#{1}", url, hash);
            }
        }

        #endregion

        public NotificationViewModel()
        {
            Type = NotificationType.Other;
            IsDeleted = false;
            CreatedOn = DateTime.UtcNow;
        }

        public NotificationViewModel(Input.Notification notification)
        {
            this.Id = notification.Id;
            this.Type = notification.Type;
            this.IsDeleted = notification.IsDeleted;
            this.CreatedOn = ApplicationHelpers.ConvertToLocalTime(notification.CreatedOn);
            this.CreatedBy = notification.CreatedBy;
            this.Post = notification.Post;
            this.Comment = notification.Comment;
            this.Giver = notification.Giver;
            this.Receiver = notification.Receiver;
        }
    }
}